//
//  YQTabBar.swift
//  WeiBO
//
//  Created by a on 16/2/29.
//  Copyright © 2016年 xinji. All rights reserved.
//

import UIKit

class YQTabBar: UITabBar {
    
    //第一步
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二部
    private func setUpUI()
    {
        addSubview(composeButton)
    }
    
    //第三部--懒加载
    lazy var composeButton: UIButton = {
        
        let buttton = UIButton(type: UIButtonType.Custom)
        
        // 设置按钮的属性
        buttton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        buttton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        buttton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        buttton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //可以添加点击事件
        
        //设置大小
        buttton.sizeToFit()
        
        return buttton
    }()
    
    /**
     在这里去修改item的frame
     */
    override func layoutSubviews() {
        super.layoutSubviews()
        /*
        [<_UITabBarBackgroundView: 0x7ff468dcc380; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7ff468d385e0>>, 
        <UITabBarButton: 0x7ff468eaaa40; frame = (2 1; 90 48); opaque = NO; layer = <CALayer: 0x7ff468c20640>>, 
        <UITabBarButton: 0x7ff468cd3260; frame = (96 1; 90 48); opaque = NO; layer = <CALayer: 0x7ff468cd3930>>, 
        <UITabBarButton: 0x7ff468cd7140; frame = (190 1; 89 48); opaque = NO; layer = <CALayer: 0x7ff468cd73c0>>, 
        <UITabBarButton: 0x7ff468eb0010; frame = (283 1; 90 48); opaque = NO; layer = <CALayer: 0x7ff468eb4bf0>>, 
        <UIImageView: 0x7ff468e783f0; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7ff468d1f5c0>>]

        */
        var index = 0
        let width = bounds.width / 5
        let height = bounds.height
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
        for subview in subviews{
            //NSClassFromString返回的是oprional的类型
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                subview.frame = CGRectMake(CGFloat(index++) * width, 0, width, height)
                if index == 2 {index++}
            }
        
        }
    }
}
